package com.xforceplus.bsstool.service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.xforceplus.bsstool.config.ConfigMain;
import com.xforceplus.bsstool.db.NewUcenterDB;
import com.xforceplus.bsstool.db.OldUcenterDB;
import com.xforceplus.bsstool.log.LogMain;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServiceStep1 {
  /**
   * 获取老运营功能集列表
   * @return
   */
  public static List<Map> getOldResourceSetList(LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_resourceset t where t.resourceset_id  ").append(ServiceCommon.getResourceSetIdInSql());
    logger.info("执行老运营资源码功能集获取，执行SQL: "+sql.toString());
    return OldUcenterDB.getSqlResult(sql.toString());
  }

  /**
   * 获取新运营功能集列表
   * @return
   */
  public static Map<String,Map> getNewResourceSetMap(LogMain logger) throws Exception {
    Map<String,Map> newResourceSetMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_resourceset t where t.resourceset_id  ").append(ServiceCommon.getResourceSetIdInSql());
    logger.info("执行新运营资源码功能集获取，执行SQL: "+sql.toString());
    List<Map> newResourceSetList = NewUcenterDB.getSqlResult(sql.toString());
    for (Map newResourceSetMap : newResourceSetList){
      newResourceSetMapMap.put(String.valueOf(newResourceSetMap.get("resourceset_id")),newResourceSetMap);
    }
    return newResourceSetMapMap;
  }

  /**
   * 比对新运营功能集
   * @return
   */
  public static boolean compareResourceSetWithNew(List<Map> oldResourceSetList,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newResourceSetMapMap  = getNewResourceSetMap(logger);
    for (Map oldResourceSetMap : oldResourceSetList){
      System.out.println("开始执行资源码功能集比对 功能集ID:"+oldResourceSetMap.get("resourceset_id"));
      logger.info("开始执行资源码功能集比对 功能集ID:"+oldResourceSetMap.get("resourceset_id"));
      //查询新运营对应功能集
      Map newResourceSetMap =newResourceSetMapMap.get(String.valueOf(oldResourceSetMap.get("resourceset_id")));
      if (newResourceSetMap == null){
        logger.warn("功能集ID:"+oldResourceSetMap.get("resourceset_id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同功能集ID,继续执行数据信息比对 新运营功能集："+ JSON.toJSONString(newResourceSetMap));
        //新老数据比对： app_id   resource_id   resourceset_name  status
        oldResourceSetMap.put("app_id",ConfigMain.APP_ID);
        result = ServiceCommon.compareMapValue(oldResourceSetMap,newResourceSetMap,
                Lists.newArrayList("app_id","resource_id","resourceset_name","status"),
                "功能集ID:"+oldResourceSetMap.get("resourceset_id"),
                logger);
      }

      //同名功能集检查
      List<Map> sameNameResourceSetList = NewUcenterDB.getSqlResult("select * from sys_resourceset where resourceset_id != "+oldResourceSetMap.get("resourceset_id")
              +" and resourceset_name = '"+oldResourceSetMap.get("resourceset_name")+"'");
      if (!CollectionUtils.isEmpty(sameNameResourceSetList)){
        logger.error("老运营功能集ID:"+oldResourceSetMap.get("resourceset_id")+" 新运营存在同名功能集记录个数："+sameNameResourceSetList.size());
        result = false;
      }
    }
    return result;
  }


  /**
   * 获取老运营功能集资源码关联关系列表
   * @return
   */
  public static List<Map> getOldResourceSetResourceRelList(LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_resourceset_resource_rel t where t.resourceset_id   ").append(ServiceCommon.getResourceSetIdInSql());
    logger.info("执行老运营资源码功能集关联关系获取，执行SQL: "+sql.toString());
    return OldUcenterDB.getSqlResult(sql.toString());
  }
  /**
   * 获取新运营功能集资源码关联关系列表
   * @return
   */
  public static Map<String,Map> getNewResourceSetResourceRelMap(LogMain logger) throws Exception {
    Map<String,Map> newResourceSetResourceRelMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_resourceset_resource_rel t where t.resourceset_id   ").append(ServiceCommon.getResourceSetIdInSql());
    logger.info("执行新运营资源码功能集关联关系获取，执行SQL: "+sql.toString());
    List<Map> newResourceSetResourceRelList = NewUcenterDB.getSqlResult(sql.toString());
    for (Map newResourceSetResourceRelMap : newResourceSetResourceRelList){
      newResourceSetResourceRelMapMap.put(String.valueOf(newResourceSetResourceRelMap.get("id")),newResourceSetResourceRelMap);
    }
    return newResourceSetResourceRelMapMap;
  }

  /**
   * 比对新运营功能集
   * @return
   */
  public static boolean compareResourceSetResourceRelWithNew(List<Map> oldResourceSetResourceRelList,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newResourceSetResourceRelMapMap  = getNewResourceSetResourceRelMap(logger);
    for (Map oldResourceSetResourceRelMap : oldResourceSetResourceRelList){
      System.out.println("开始执行资源码功能集关联关系比对 功能集关联关系ID:"+oldResourceSetResourceRelMap.get("id"));
      logger.info("开始执行资源码功能集关联关系比对 功能集关联关系ID:"+oldResourceSetResourceRelMap.get("id"));
      //查询新运营对应功能集
      Map newResourceSetResourceRelMap =newResourceSetResourceRelMapMap.get(String.valueOf(oldResourceSetResourceRelMap.get("id")));
      if (newResourceSetResourceRelMap == null){
        logger.warn("功能集关联关系ID:"+oldResourceSetResourceRelMap.get("id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同功能集关联关系ID,继续执行数据信息比对 新运营功能集关联关系："+ JSON.toJSONString(newResourceSetResourceRelMap));
        //新老数据比对： resource_id   resourceset_id
        result = ServiceCommon.compareMapValue(oldResourceSetResourceRelMap,newResourceSetResourceRelMap,
                Lists.newArrayList("resource_id","resourceset_id"),
                "功能集关联关系ID:"+oldResourceSetResourceRelMap.get("id"),
                logger);
      }

      //重复功能集关联关系检查
      List<Map> sameResourceSetResourceRelList = NewUcenterDB.getSqlResult("select * from sys_resourceset_resource_rel where id != "+oldResourceSetResourceRelMap.get("id")
              +" and resource_id = "+oldResourceSetResourceRelMap.get("resource_id")
              +" and resourceset_id = "+oldResourceSetResourceRelMap.get("resourceset_id"));
      if (!CollectionUtils.isEmpty(sameResourceSetResourceRelList)){
        logger.error("老运营功能集关联关系ID:"+oldResourceSetResourceRelMap.get("id")+" 新运营存在重复功能集关联关系记录个数："+sameResourceSetResourceRelList.size());
        result = false;
      }
    }
    return result;
  }




  /**
   * 获取老运营资源码列表
   * @return
   */
  public static List<Map> getOldResourceList(LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_resource t where t.resource_id "
            +"in(select resource_id from sys_resourceset_resource_rel  "
            +"where resourceset_id   ").append(ServiceCommon.getResourceSetIdInSql()).append(")");
    logger.info("执行老运营资源码获取，执行SQL: "+sql.toString());
    return OldUcenterDB.getSqlResult(sql.toString());
  }
  /**
   * 获取新运营资源码列表
   * @return
   */
  public static Map<String,Map> getNewResourceMap(LogMain logger) throws Exception {
    Map<String,Map> newResourceMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("select * from sys_resource t where t.resource_id "
            +"in(select resource_id from sys_resourceset_resource_rel  "
            +"where resourceset_id   ").append(ServiceCommon.getResourceSetIdInSql()).append(")");
    logger.info("执行新运营资源码获取，执行SQL: "+sql.toString());
    List<Map> newResourceList = NewUcenterDB.getSqlResult(sql.toString());
    for (Map newResourceMap : newResourceList){
      newResourceMapMap.put(String.valueOf(newResourceMap.get("resource_id")),newResourceMap);
    }
    return newResourceMapMap;
  }

  /**
   * 比对新运营功能集
   * @return
   */
  public static boolean compareResourceWithNew(List<Map> oldResourceList,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newResourceMapMap  = getNewResourceMap(logger);
    for (Map oldResourceMap : oldResourceList){
      System.out.println("开始执行资源码比对 资源码ID:"+oldResourceMap.get("resource_id"));
      logger.info("开始执行资源码比对 资源码ID:"+oldResourceMap.get("resource_id"));
      //查询新运营对应功能集
      Map newResourceMap =newResourceMapMap.get(String.valueOf(oldResourceMap.get("resource_id")));
      if (newResourceMap == null){
        logger.warn("资源码ID:"+oldResourceMap.get("resource_id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同资源码ID,继续执行数据信息比对 新运营资源码："+ JSON.toJSONString(newResourceMap));
        //新老数据比对： app_id resource_code   resource_name  resource_type status
        oldResourceMap.put("app_id",ConfigMain.APP_ID);
        result = ServiceCommon.compareMapValue(oldResourceMap,newResourceMap,
                Lists.newArrayList("app_id","resource_code","resource_name","resource_type","status"),
                "资源码ID:"+oldResourceMap.get("resource_id"),
                logger);
      }

      //重复资源码检查
      List<Map> sameResourceList = NewUcenterDB.getSqlResult("select * from sys_resource where resource_id != "+oldResourceMap.get("resource_id")
              +" and app_id = "+ ConfigMain.APP_ID
              +" and resource_code = '"+oldResourceMap.get("resource_code")+"'");
      if (!CollectionUtils.isEmpty(sameResourceList)){
        logger.error("老运营资源码ID:"+oldResourceMap.get("resource_id")+" 新运营存在重复资源码(resource_code重复)记录个数："+sameResourceList.size());
        result = false;
      }
      /*
      sameResourceList = NewUcenterDB.getSqlResult("select * from sys_resource where resource_id != "+oldResourceMap.get("resource_id")
              +" and resource_name = '"+oldResourceMap.get("resource_name")+"'");
      if (!CollectionUtils.isEmpty(sameResourceList)){
        logger.error("老运营资源码ID:"+oldResourceMap.get("resource_id")+" 新运营存在重复资源码(resource_name重复)记录个数："+sameResourceList.size());
        result = false;
      }*/
    }
    return result;
  }
}
